<?php

namespace app\api\controller\v48;

use app\aliyun\controller\AliyunPhone;
use app\api\controller\v2\Auth;
use app\astrologer\lib\AstrPush;
use app\chat\LiveMessage;
use app\common\cachemodel\consult\AppConsultGift;
use app\common\cachemodel\consult\AppUserConsultGift;
use app\common\cachemodel\OrdersLib;
use app\common\cachemodel\UserCoupon;
use app\common\cachemodel\ZhunleUserAccount;
use app\common\cachemodel\ZhunleUserAccountRecord;
use app\common\cachemodel\zl8\Zl8UserChatReservationRecord;
use app\common\cachemodel\zlcr3\Zlcr3ConsultAccount;
use app\common\cachemodel\ZlcrAstr;
use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use app\zyhsapi\model\ZyhsConfig;
use app\zyhsapi\model\ZyhsConsultAstrData;
use app\zyhsapi\model\ZyhsConsultChatRecord;
use app\zyhsapi\model\ZyhsConsultChatRecordAstr;
use app\zyhsapi\model\ZyhsConsultChatRecordDetails;
use app\zyhsapi\model\ZyhsConsultChatRtcRecord;
use app\zyhsapi\model\ZyhsConsultChatRtcRecordComment;
use app\zyhsapi\model\ZyhsConsultQuestion;
use app\zyhsapi\model\ZyhsConsultRtcUserBenefits;
use think\Db;
use think\response\Json;

class UserConsultChat extends Auth
{
    private $appType2 = 2;

    /**
     * 获取 咨询礼包详情
     * @return Json
     */
    public function giftDetail()
    {
        $userGiftId = input('gift_id/d', 0);
        if (!$userGiftId)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $appUserConsultGiftModel = new AppUserConsultGift();
        $userGiftPackInfo = $appUserConsultGiftModel->getInfoById($userGiftId);
        if (!$userGiftPackInfo || $userGiftPackInfo['uid'] !== $this->user['id'])
        {
            return show(config('code.error'), '服务异常，礼包不存在');
        }
        if ($userGiftPackInfo['status'] === 1 && $userGiftPackInfo['end_time'] > 0 && $userGiftPackInfo['end_time'] <= $this->request->time())
        {
            $appUserConsultGiftModel->updateData($this->user['id'], $userGiftPackInfo['id'], ['status' => 3, 'update_time' => $this->request->time()]);
            $userGiftPackInfo['status'] = 3;
            $userGiftPackInfo['update_time'] = $this->request->time();
        }

        $resData = $appUserConsultGiftModel->resertGiftPackInfo($userGiftPackInfo);

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 我的咨询礼包列表
     * @return Json
     * @throws \Exception
     */
    public function myGiftList()
    {
        $type = input('type/d', 0);
        if (!in_array($type, [1, 2], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $appUserConsultGiftModel = new AppUserConsultGift();
        $sortKey = $type === 1 ? 'id' : 'update_time';
        $pageData = $appUserConsultGiftModel->getListPage($this->user['id'], $sortKey, [], $type);
        $dataList = $appUserConsultGiftModel->dataInit($pageData['data']);

        if ($type === 1)
        {
            $list = [];
            foreach ($dataList as $k => $item)
            {
                if ($item['end_time'] > 0 && $item['end_time'] <= $this->request->time())
                {   // 该优惠券过期了
                    $appUserConsultGiftModel->updateData($this->user['id'], $item['id'], ['status' => 3, 'update_time' => $this->request->time()]);
                    continue;
                }
                $list[] = $item;
            }
            $dataList = $list;
        }

        $pageData['data'] = $appUserConsultGiftModel->resertPageDataList($dataList);

        return show(config('code.success'), '请求成功', $pageData);
    }

    /**
     * 获取 占星师可用礼包
     * @return Json
     * @throws \Exception
     */
    public function getAstrUsableGift()
    {
        $astrId = input('astr_id/d', 0);
        if (!$astrId)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $consultAccountInfo = (new Zlcr3ConsultAccount())->getInfoById($astrId);
        $isConsult = $consultAccountInfo && $consultAccountInfo['consult_status'] === 1 ? 1 : 0;
        if (!$isConsult)
        {
            return show(config('code.error'), '服务异常，咨询师不存在～');
        }
        // 获取 可用礼包列表
        $appUserConsultGiftModel = new AppUserConsultGift();
        $pageData = $appUserConsultGiftModel->getListPage($this->user['id'], 'id', [], 1);
        $dataList = $appUserConsultGiftModel->dataInit($pageData['data']);
        foreach ($dataList as $k => $item)
        {
            if ($item['end_time'] === 0)
            {
                $dataList[$k]['end_time'] = 2145888000;
            }
        }
        $dataList = resort($dataList, 'end_time', 'asc');
        $resData = [
            'list1' => [],  //  可用礼包列表
            'list2' => [],  //  不可以礼包列表
        ];
        $appConsultGiftModel = new AppConsultGift();
        $consultGiftList = [];
        foreach ($dataList as $item)
        {
            if ($item['end_time'] > 0 && $item['end_time'] <= $this->request->time())
            {   // 礼包过期了
                $appUserConsultGiftModel->updateData($this->user['id'], $item['id'], ['status' => 3, 'update_time' => $this->request->time()]);
                continue;
            }

            if (isset($consultGiftList[$item['gift_id']]))
            {
                $consultGiftInfo = $consultGiftList[$item['gift_id']];
            } else
            {
                $consultGiftInfo = $appConsultGiftModel->getInfoById($item['gift_id'], ['name', 'img', 'exclude_astrs', 'minute']);
                $consultGiftList[$item['gift_id']] = $consultGiftInfo;
            }
            $itemData = [
                'img' => '/test/consult_chat/consult_gift_pack_icon.png',
                'title' => $consultGiftInfo['name'],
                'desc' => sprintf("时长：%d分钟", $consultGiftInfo['minute']),
                'remaining_time' => $item['duration_minute'] - $item['use_minute'],
                'gift_id' => $item['id'],
            ];

            if (in_array($astrId, $consultGiftInfo['exclude_astrs'], true))
            {   // 不可用
                $resData['list2'][] = $itemData;
            } else
            {   // 可用
                $resData['list1'][] = $itemData;
            }
        }

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 购买连麦礼包
     * @return Json
     * @throws ApiException
     * @throws \Exception
     */
    public function buyGift()
    {
        $id = input('id/d', 0);
        $password = input('password/s', 0);
        $appConsultGiftModel = new AppConsultGift();
        $appConsultGiftModel->setPage(-1);
        $giftList = $appConsultGiftModel->getListPage(0)['data'];
        $giftList = array_column($giftList, null, 'id');
        $gift = $giftList[$id] ?? [];
        if (!$gift)
        {
            return show(config('code.error'), '礼包不存在');
        }
        //获取准币余额
        $zhunleUserAccountModel = new ZhunleUserAccount();
        $userCoinInfo = $zhunleUserAccountModel->getUserCoinFromCache($this->user['id']);
        $user_coin = $userCoinInfo ? (int)$userCoinInfo['balance'] : 0;
        // 进行支付密码验证
        if ($userCoinInfo['password'])
        {
            $zhunleUserAccountModel->verifyCoinPay($userCoinInfo, $password, $gift['coin']);
        }
        //检测余额是否足够
        if ($user_coin < $gift['coin'])
        {
            return show(410, '准币不足，请前去充值');
        }
        $orderType = 33;
        $order_type_des = OrdersLib::createOrderId($orderType);
        $price = $gift['coin'] / 10;
        $insertData = [
            'status' => 1,
            'account' => 0,
            'uid' => $this->user['id'],
            'consumer_type' => 2,
            'order_type' => $orderType,
            'gid' => $id,
            'pray_date' => 0,
            'rid' => 0,
            'total_amount' => $price,//实际收入
            'pay_price' => $price,//用户支付
            'coin_num' => $gift['coin'],
            'balance' => $userCoinInfo['balance'] - $gift['coin'],
            'order_id' => $order_type_des['order_id'],
            'trade_type' => 4,
            'create_time' => time(),
            'transaction_id' => '',
        ];
        //增加准币明细记录
        $reId = (new ZhunleUserAccountRecord())->SaveData($this->user['id'], $insertData);
        if ($reId)
        {
            //礼包购买数自增
            $appConsultGiftModel->where('id', $gift['id'])->inc('num')->update();
            //用户礼包购买记录
            $appUserConsultGiftModel = new AppUserConsultGift();
            $userGiftId = $appUserConsultGiftModel->saveData(
                [
                    'uid' => $this->user['id'],
                    'status' => 1,  // 状态 1=>使用中 2=>已用尽 3=>已过期
                    'gift_id' => $id,
                    'unit_price' => (int)floor($gift['coin'] / $gift['minute']), // 连线礼包 连线时长单价 小数部分 向下取整
                    'start_time' => $this->request->time(),
                    'end_time' => $gift['expiration_time'] ? strtotime("+{$gift['expiration_time']}days", $this->request->time()) : 0,
                    'duration_minute' => $gift['minute'],
                    'order_num' => $order_type_des['order_id'],
                    'type' => 1,
                    'create_time' => $this->request->time(),
                ]
            );
            if ($userGiftId)
            {
                $balance = $userCoinInfo['balance'] - $gift['coin'];
                $consume_price = $userCoinInfo['consume_price'] + $price;
                $consume_coin = $userCoinInfo['consume_coin'] + $gift['coin'];
                //更新用户钱包金额
                $zhunleUserAccountModel->UpdateData($this->user['id'], ['balance' => $balance, 'consume_price' => $consume_price, 'consume_coin' => $consume_coin, 'update_time' => time()], ['uid' => $this->user['id']]);

                $appUserConsultGiftModel->setRecentPayGiftUserInfo([
                    'id' => $userGiftId,
                    'nickname' => $this->user['nickname'],
                    'uid' => $this->user['id'],
                    'gift_id' => $id, // 礼包商品ID
                ]);

                return show(config('code.success'), '购买成功', ['order_id' => $order_type_des['order_id'], 'gift_id' => $userGiftId]);
            }
        }
        return show(config('code.error'), '购买失败');
    }

    /**
     * 发起 RTC连麦 交互请求
     * @return Json
     * @throws \Exception
     */
    public function requestRtcInteraction()
    {
        $astrId = input('astr_id/d', 0);
        $giftId = input('gift_id/d', 0);
        $zlcrAstrModel = new ZlcrAstr();
        if (Util::commentCheck($this->user['id'], ['action' => 'requestRtcInteraction', 'astr_id' => $astrId, 'gift_id' => $giftId], 5))
        {
            return show(config('code.error'), '操作频繁，请稍后再试～');
        }
        $consultAccountInfo = (new Zlcr3ConsultAccount())->getInfoById($astrId);
        $isConsult = $consultAccountInfo && $consultAccountInfo['consult_status'] === 1 ? 1 : 0;
        if (!$isConsult)
        {
            return show(config('code.error'), '服务异常，咨询师不存在～');
        }
        // 获取当前用户 咨询状态
        $consultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
        $userNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(1, $this->appType2, $this->user['id']);
        if ($userNowConsultRtcInfo)
        {   // 用户正在连麦中
            return show(config('code.error'), '当前正在连线中，不可重复连线～');
        }
        // 咨询师营业状态判断 不是ws在线状态
        if ($consultAccountInfo['status'] !== 1)
        {   // 咨询师休息 了
            return show(config('code.error'), '咨询师暂时不在，稍侯再来吧~');
        }
        if ($giftId)
        {
            $userGiftIngo = (new AppUserConsultGift())->getInfoById($giftId);
            if (!$userGiftIngo || $userGiftIngo['uid'] !== $this->user['id'] || $userGiftIngo['status'] !== 1 || $userGiftIngo['duration_minute'] - $userGiftIngo['use_minute'] <= 0)
            {
                return show(config('code.error'), '服务异常，礼包不存在或已过期～');
            }
            if ($userGiftIngo['end_time'] > 0 && $userGiftIngo['end_time'] < $this->request->time())
            {
                return show(config('code.error'), '服务异常，礼包已过期～');
            }
        }
        // 获取当前咨询师 咨询状态
        $astrNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(2, $this->appType2, $astrId);
        if ($astrNowConsultRtcInfo)
        {   // 咨询师 当前正在 连麦中 或者 已经有连麦请求
            $extend = [
                'rtc_data' => [
                    'rtc_id' => 0,
                    'status' => 5,  //【rtc_id=0该字段数据生效】 1=>申请中待接听  2=>咨询师未响应  3=>咨询师拒绝  4=>连线已取消 5=>咨询师忙线中
                ],
                'version' => $this->headers['version'] ?? '',
            ];
            $isRtc = false;
        } else
        {
            $extend = [
                'rtc_data' => [
                    'rtc_id' => 0,
                    'status' => 1,  //【rtc_id=0该字段数据生效】 1=>申请中待接听  2=>咨询师未响应  3=>咨询师拒绝  4=>连线已取消 5=>咨询师忙线中
                ],
                'version' => $this->headers['version'] ?? '',
            ];
            $isRtc = true;
        }
        $consultChatIdModel = new ZyhsConsultChatRecord();
        $chatId = $consultChatIdModel->getChatIdByAstrId($this->appType2, $this->user['id'], $astrId);
        $nowTimw = $this->request->time();
        if (!$chatId)
        {
            $insertData = [
                'app_type' => $this->appType2,
                'uid' => $this->user['id'],
                'astr_id' => $astrId,
                'last_chat_detail_id' => 0,
                'last_chat_detail_time' => 0,
                'create_time' => $nowTimw,
            ];
            // 创建新的会话
            $chatId = $consultChatIdModel->SaveData($insertData);
        }

        $consultMsgData = [
            'chat_id' => $chatId,
            'app_type' => $this->appType2,
            'uid' => $this->user['id'],
            'astr_id' => $astrId,
            'type' => 2,    //  1=>APP消息 2=>用户发送消息
            'title' => '',
            'content' => '', // 提问问题
            'data_type' => 3,   // 1=>普通消息  2=>图片  3=>语音咨询消息
            'extend' => $extend,  //  语音咨询消息 ｜ 图片信息
            'pid' => 0, // 待更新
            'create_time' => $nowTimw,
        ];

        $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();
        $resData = $consultChatRecordDetailsModel->SaveData($chatId, $consultMsgData);
        if ($resData)
        {
            $consultChatIdModel->updateData($chatId, [
                'uid' => $this->user['id'],
                'last_chat_detail_id' => $resData['id'],
                'last_chat_detail_time' => $nowTimw,
            ], $this->appType2);
            // 更新咨询师端 最后咨询信息
            (new ZyhsConsultChatRecordAstr())->userSendUpdate($this->appType2, $this->user['id'], $astrId, [
                'last_chat_detail_id' => $resData['id'],
                'last_chat_detail_time' => $nowTimw,
                'unread_num' => Db::raw('unread_num + 1'),
                'call_num' => Db::raw('call_num + 1'),
            ]);

            $resData2 = current($consultChatRecordDetailsModel->userDataListResert([$resData], [], $this->user, $this->appType2));
            if ($isRtc)
            {   // 更新用户端 最后咨询信息
                $astrInfo = $zlcrAstrModel->getInfoById($astrId, ['avatar', 'nickname', 'tel']);
                $userNowConsultRtcInfo = [
                    'id' => 0, // 连麦ID    ID==0 为邀请连麦  否则为主播接受连麦
                    'rtc_sign' => uniqid('', false),
                    'app_type' => $this->appType2,
                    'astr_id' => $astrId,
                    'nickname' => $astrInfo['nickname'] ?? '',
                    'avatar' => $astrInfo['avatar'] ?? '',
                    'status' => 1,  // 当前连麦状态 1=>等待咨询师接受 2=>咨询师接受开始连麦中 3=>连麦成功 连麦中
                    'request_time' => $this->request->time(),   // 邀请咨询师连麦时间
                    'chat_id' => $resData['id'],
                    'gift_id' => $giftId,
                ];
                $consultChatRtcRecordModel->saveNowRtcDataToCache(1, $this->appType2, $this->user['id'], $userNowConsultRtcInfo, false, $resData['id']);
                $astrNowConsultRtcInfo = [
                    'id' => 0, // 连麦ID    ID==0 为邀请连麦  否则为主播接受连麦
                    'rtc_sign' => $userNowConsultRtcInfo['rtc_sign'],
                    'app_type' => $this->appType2,
                    'uid' => $this->user['id'],
                    'nickname' => $this->user['nickname'] ?? '',
                    'avatar' => $this->user['avatar'] ?? '',
                    'uen' => $this->user['uen'] ?? '',
                    'status' => 1,  // 当前连麦状态 1=>等待咨询师接受 2=>咨询师接受开始连麦中 3=>连麦成功 连麦中
                    'request_time' => $this->request->time(),   // 邀请咨询师连麦时间
                    'chat_id' => $resData['id'],
                    'gift_id' => $giftId,
                ];
                $astrNowConsultRtcInfo['recode_datas'] = $consultChatRecordDetailsModel->getRtcInitRecodeInfo($chatId);
                $consultChatRtcRecordModel->saveNowRtcDataToCache(2, $this->appType2, $astrId, $astrNowConsultRtcInfo, false, $resData['id']);
                // 判断咨询师是否在线
                if (LiveMessage::getUserIsOnline(2, $astrId))
                {   // 在线
                    $astrData = current($consultChatRecordDetailsModel->astrDataListResert([$resData], [], $this->user, $this->appType2));
                    LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $astrId), LiveMessage::MSG_TYPE_POP_CONSULT_MSG, [
                        'msg_type' => 2,
                        'app_type' => $this->appType2,
                        'uid' => $this->user['id'],
                        'msg_data' => $astrData,
                    ]);
                    $newUserWelfare = (new ZyhsConsultRtcUserBenefits())->getConsultRtcUserBenefits($this->appType2, $this->user['id']);
                    $freeRtcMin = $newUserWelfare['min'] ?? 0;
                    $desc = $freeRtcMin ? sprintf("申请语音咨询(免费%d分钟)…", $freeRtcMin) : '申请语音咨询…';
                    $voiceCallData = [
                        'chat_id' => $resData['id'],
                        'app_type' => $this->appType2,
                        'uid' => $this->user['id'],
                        'avatar' => $this->user['avatar'],
                        'nickname' => $this->user['nickname'],
                        'status' => 1,
                        'desc' => $desc,
                    ];
                    LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_ASTRO, 0, $astrId), LiveMessage::MSG_TYPE_POP_VOICE_CONSULT_CALL, $voiceCallData);
                } else
                {   // 咨询师ws不在线
                    AliyunPhone::singleCallByTts($astrInfo['tel'], 'TTS_282195080');
                }
                // 2分钟后 判断状态是否还是待接听 状态 如果是待接听状态 则取消连线
                Util::queuePush('rtc_consult_accept_check', ['id' => $resData['id']], 120);
            }
            $pushData = ['action' => 'extend', 'target' => 'local', 'link' => 'empty',];
            (new AstrPush())->pushUserMsg($astrId, '你收到新的咨询连线申请，点击查看', $pushData);
            $resData2['time_des'] = '';
            //  增加咨询师 可回复次数
            $zyhsConsultChatRecordAstrModel = new ZyhsConsultChatRecordAstr();
            $astrChatId = $zyhsConsultChatRecordAstrModel->getIdByAstrIdAndUid($astrId, $this->appType2, $this->user['id']);
            $astrChatIdInfo = $zyhsConsultChatRecordAstrModel->getInfoById($astrChatId);
            if ($astrChatIdInfo['call_delay_num'] < 3)
            {
                $astrChatIdInfo['call_delay_num'] = 3;
                $zyhsConsultChatRecordAstrModel->UpdateData($astrChatId, $astrId, [
                    'call_delay_num' => 3,
                ]);
            }
            $this->handleReservationStatus();
            return show(config('code.success'), '请求成功', $resData2);
        }

        return show(config('code.error'), '服务异常，请稍后再试～');
    }

    /**
     * 处理预约状态
     * @return void
     */
    private function handleReservationStatus()
    {
        try
        {
            // 获取当前用户的预约记录
            $zl8UserChatReservationRecordModel = new Zl8UserChatReservationRecord();
            $userReservationList = $zl8UserChatReservationRecordModel->getUserReservationList($this->user['id']);
            if ($userReservationList)
            {
                $ids = array_keys($userReservationList);
                $dateTmp = $this->request->time();
                $userReservationInfo = $zl8UserChatReservationRecordModel->batchGetInfoByIds($ids);
                foreach ($userReservationInfo as $item)
                {
                    //是否为待履约状态
                    if ($item['status'] === $zl8UserChatReservationRecordModel->statusList['wait_keep_promise'])
                    {
                        $beforeHourTmp = $item['h_id'] === 24 ? 23 : $item['h_id'] - 1;
                        $dateTimeTmp = strtotime($item['date'] . ' ' . $item['h_id'] . ':00');
                        $beforeDateTimeTmp = strtotime($item['date'] . ' ' . $beforeHourTmp . ':00');
                        //在预约时间段内处理预约状态
                        if ($dateTmp >= $beforeDateTimeTmp && $dateTmp < $dateTimeTmp)
                        {
                            $res = $zl8UserChatReservationRecordModel->updateData($item['id'], ['status' => $zl8UserChatReservationRecordModel->statusList['keep_promise'], 'update_time' => $dateTmp]);
                            if ($res)
                            {
                                $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();
                                $chatDetail = $consultChatRecordDetailsModel->getInfoById($item['char_id']);
                                $chatDetail['extend'] ['reservation_info']['status'] = $zl8UserChatReservationRecordModel->statusList['keep_promise'];
                                $consultChatRecordDetailsModel->UpdateData($chatDetail['id'], ['extend' => $chatDetail['extend']]);
                            }
                        }
                    }
                }
            }
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()用户连线,履约处理异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
        }
    }

    /**
     * 【立即｜更换】咨询
     * @return Json
     * @throws \Exception
     */
    public function getAstrAsk()
    {
        $astrId = input('astr_id/d', 0);

        $testAccountConfig = (new ZyhsConfig())->configInfo('rtc');
        $testUser = $testAccountConfig['test_user'] ?? '';
        $testAstrId = $testUser ? explode(',', $testUser) : [];
        if (Util::ZyhsIsHuaweiOrHonorOrOppoHg(2))
        {   // 华为 合规情况 只显示 当前占星师数据
            if (Util::IsHuaweiAndOppo())
            {
                $astrIdList = $testAccountConfig['huawei_hg_consult_astr_ids'] ? explode(',', $testAccountConfig['huawei_hg_consult_astr_ids']) : [];
            } else
            {
                $astrIdList = $testAccountConfig['honor_hg_consult_astr_ids'] ? explode(',', $testAccountConfig['honor_hg_consult_astr_ids']) : [];
            }
            $astrIds = [];
            foreach ($astrIdList as $idItem)
            {
                if (!in_array($idItem, $testAstrId) && $astrId != $idItem)
                {
                    $astrIds[] = (int)$idItem;
                }
            }
            $data = $astrIds;
        } else
        {
            $data = (new ZyhsConsultAstrData())->getRankingList(2);
            if ($astrId && !isset($data[$astrId]))
            {
                $astrId = 0;
            } else
            {
                unset($data[$astrId]);
            }
            foreach ($testAstrId as $value)
            {
                unset($data[$value]);
            }
            $data = array_keys($data);
        }
        shuffle($data);
        $resAstrId = 0;
        $resData = [];
        $zlcrAstrModel = new ZlcrAstr();
        $zlcr3ConsultAccountModel = new Zlcr3ConsultAccount();
        $consultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
        if ($consultChatRtcRecordModel->getNowRtcDataByCache(1, $this->appType2, $this->user['id']))
        {
            return show(202, '正在连线中，不支持当前操作');
        }
        if (Util::ZyhsIsHuaweiOrHonorOrOppoHg(2))
        {
            $rtcConfig = (new ZyhsConfig())->configInfo('rtc');
            if (Util::IsHuaweiAndOppo())
            {
                if (isset($rtcConfig['huawei_work_intro']) && $rtcConfig['huawei_work_intro'])
                {
                    $workIntro = $rtcConfig['huawei_work_intro'];
                }
            } else
            {
                if (isset($rtcConfig['honor_work_intro']) && $rtcConfig['honor_work_intro'])
                {
                    $workIntro = $rtcConfig['honor_work_intro'];
                }
            }
        }
        while ($resAstrId === 0 && $data)
        {
            $astrIdItem = array_shift($data);
            $consultAstrInfo = $zlcr3ConsultAccountModel->getInfoById($astrIdItem);
            if ($consultAstrInfo['status'] === 1 && $consultAstrInfo['consult_status'] === 1)
            {   // 咨询师在线 并且 咨询状态正常
                // 判断是否正在连线
                $astrNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(2, $this->appType2, $astrIdItem);
                if (!$astrNowConsultRtcInfo)
                {
                    $resAstrId = $astrIdItem;
                    $astrInfo = $zlcrAstrModel->getInfoById($astrIdItem);
                    $coinNum = OrdersLib::getCoinNum($this->user['id']);  // 账户信息
                    $tip = '';
                    if ($consultAstrInfo['service_duration'] > 0)
                    {
                        $t = ceil($consultAstrInfo['service_duration'] / 60);
                        $tip .= '服务' . ($t > 200 ? '200+' : $t) . '分钟';
                    }
                    if ($consultAstrInfo['recommended_nums'] > 0)
                    {
                        if ($tip)
                        {
                            $tip .= '·';
                        }
                        $tip .= "{$consultAstrInfo['recommended_nums']}次被推荐";
                    }
                    $couponIds = (new UserCoupon())->getConsultRtcCids($this->user['id']);
                    // 获取新人福利 免充值 发起连线
                    $newUserWelfare = (new ZyhsConsultRtcUserBenefits())->getConsultRtcUserBenefits($this->appType2, $this->user['id']);
                    $freeRtcDuration = $newUserWelfare ? (($newUserWelfare['min'] ?? 0) * 60) : 0;
                    // 判断当前是否 已经 提交过了
                    $zlcr3ConsultQuestionModel = new ZyhsConsultQuestion();
                    $consultQuestionInfo = $zlcr3ConsultQuestionModel->getUserConsultQuestion($this->appType2, $this->user['id']);
                    $tags = $zlcr3ConsultAccountModel->getTags($consultAstrInfo['tags']);
                    if (Util::ZyhsIsHuaweiOrHonorOrOppoHg(2))
                    {
                        $tags = array_values(array_diff($tags, ['星盘']));
                    }
                    $resData = [
                        'avatar' => $astrInfo['avatar'],
                        'name' => $astrInfo['nickname'],
                        'tags_icon' => $zlcr3ConsultAccountModel->getTagsIcon($consultAstrInfo['tids']),
                        'tags' => $tags,
                        'user_coin' => $coinNum,
                        'price_coin' => $consultAstrInfo['price'],
                        'tip' => $tip,
                        'notice' => "你已确认自己为成年人，无精神疾病和自我伤害等倾向，并承诺在咨询过程中理性看待老师给出的建议，在咨询以外的一切其它行为与平台无关。\r\n连线需保证您的准币余额大于五分钟连麦消耗。\r\n请提前添加咨询所需的档案信息，以免浪费咨询时间。\r\n连麦开始1分钟内是免费试麦准备时间，如果出现声音问题请及时申请故障挂断，调试后重试。\r\n请保证网络正常，以免因网络问题影响你的咨询体验\r\n连麦咨询不支持退款，请谨慎购买，理性消费:为保证咨询服务的完整性，请尽量保证准币余额充足。",
                        'des' => $workIntro ?? ($astrInfo['extend']['work_intro'] ?? ''),    // 营业介绍
                        'btn_text' => $freeRtcDuration > 60 ? sprintf("免费咨询连线%d分钟", $newUserWelfare['min'] ?? 0) : '发起咨询(免费60秒)',
                        'astr_id' => $astrIdItem,
                        'has_coupon' => $couponIds ? 1 : 0,
                        'close_is_pop' => $consultQuestionInfo ? 0 : 1,
                        'level' => $consultAstrInfo['level'],
                        'has_astro_skill' => in_array(116, $consultAstrInfo['tags'], false) ? 1 : 0, // 是否具备 星盘 资格 1=>具备 0=>不具备
                        'free_rtc_duration' => $freeRtcDuration,
                    ];
                }
            }
        }
        if ($resData)
        {
            $appUserConsultGiftModel = new AppUserConsultGift();
            $GiftPageData = $appUserConsultGiftModel->getListPage($this->user['id'], 'id', [], 1);
            $resData['has_gift_pack'] = $GiftPageData['data'] ? 1 : 0;
            return show(config('code.success'), '请求成功', $resData);
        } else
        {
            if ($astrId)
            {
                return show(202, '当前咨询师较少，没有更多了');
            } else
            {
                return show(202, '当前咨询师较少，请稍后再试～');
            }
        }
    }

    /**
     * 获取连线咨询状态
     * @return Json
     * @throws \Exception
     */
    public function getRtcConsultStatusInfo()
    {
        $astrId = input('astr_id/d', 0);
        $chatDetailId = input('id/d', 0);

        $zlcrAstrModel = new ZlcrAstr();
        $zlcr3ConsultAccountModel = new Zlcr3ConsultAccount();
        $consultChatRtcRecordModel = new ZyhsConsultChatRtcRecord();
        $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();

        $chatDetail = null;
        if ($chatDetailId)
        {
            $chatDetail = $consultChatRecordDetailsModel->getInfoById($chatDetailId);
            if (!$chatDetail)
            {
                return show(config('code.error'), '请求异常，咨询信息不存在～');
            }

            $userNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(1, $this->appType2, $this->user['id']);
            if ($userNowConsultRtcInfo && (int)$userNowConsultRtcInfo['chat_id'] === $chatDetailId && $userNowConsultRtcInfo['status'] < 3)
            {
                $chatDetail = null;
            }
        } else
        {
            $consultAccountInfo = (new Zlcr3ConsultAccount())->getInfoById($astrId);
            $isConsult = $consultAccountInfo && $consultAccountInfo['consult_status'] === 1 ? 1 : 0;
            if (!$isConsult)
            {
                return show(config('code.error'), '服务异常，咨询师不存在');
            }
            // 获取当前 连线信息
            $userNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(1, $this->appType2, $this->user['id']);
            if ($userNowConsultRtcInfo)
            {   // 用户当前连线信息
                if ((int)$userNowConsultRtcInfo['astr_id'] !== $astrId)
                {
                    return show(config('code.error'), '你当前正在连线中，不可重复连线。');
                }
                if ((int)$userNowConsultRtcInfo['status'] === 3)
                {    // 当前连麦状态 1=>等待咨询师接受 2=>咨询师接受开始连麦中 3=>连麦成功 连麦中
                    // 当前正在连麦中
                    $chatDetail = $consultChatRecordDetailsModel->getInfoById($userNowConsultRtcInfo['chat_id']);
                }
                $chatDetailId = (int)$userNowConsultRtcInfo['chat_id'];
            }
        }

        $UserAccountModel = new ZhunleUserAccount();
        if ($chatDetail)
        {
            $rtcId = $chatDetail['extend']['rtc_data']['rtc_id'] ?? 0;
            if (!$rtcId)
            {
                return show(config('code.error'), '服务异常，请稍后再试～');
            }
            $avatar = $userNowConsultRtcInfo['avatar'] ?? '';
            $nickname = $userNowConsultRtcInfo['nickname'] ?? '';
            if (!$userNowConsultRtcInfo)
            {
                $astrInfo = $zlcrAstrModel->getInfoById($astrId, ['avatar', 'nickname']);
                $avatar = $astrInfo['avatar'];
                $nickname = $astrInfo['nickname'];
            }
            // 获取当前 连线信息
            $rtcInfo = $consultChatRtcRecordModel->getInfoById($rtcId);

            switch ($rtcInfo['status'])
            {   //  2=>连接中  3=>连麦中 4=>连麦结束 5=>故障挂断 6=>取消连麦（连接中挂断）
                case 3: // 3=>连麦中
                    $astrNowConsultRtcInfo = $consultChatRtcRecordModel->getNowRtcDataByCache(2, $this->appType2, $astrId);
                    if (isset($astrNowConsultRtcInfo['gift_id']) && $astrNowConsultRtcInfo['gift_id'])
                    {   // 使用了 连线咨询礼包
                        $userGiftInfo = (new AppUserConsultGift())->getInfoById($astrNowConsultRtcInfo['gift_id']);
                        $giftDuration = ($userGiftInfo['duration_minute'] - $userGiftInfo['use_minute']) * 60;
                    }
                    $user_coin = $UserAccountModel->getUserCoinFromCache($this->user['id']);  // 账户信息
                    $coinNum = (int)($user_coin['balance'] ?? 0);// 准币余额
                    $resData = [
                        'status' => 21,
                        'duration' => $this->request->time() - $rtcInfo['start_time'],
                        'test_duration' => $rtcInfo['test_duration'],
                        'user_coin' => $coinNum,                // 用户剩余准币数量
                        'used_coin' => $rtcInfo['coin'],        // 用户已经使用准币数量
                        'price_coin' => $rtcInfo['unit_price'], // 单价
                        'details' => $consultChatRtcRecordModel->resetRtcConsultStatusData($astrNowConsultRtcInfo ?? []),
                        'free_rtc_duration' => $rtcInfo['extend']['free_rtc_duration'] ?? 0,    // 新人福利 免费连线时长【秒】
                        'gift_duration' => $giftDuration ?? 0,  // 礼包可用时长【秒】 0代码没有使用礼包
                    ];
                    break;
                case 4: // 4=>连麦结束
                case 5: // 5=>故障挂断
                    if ($rtcInfo['duration'] >= 300)
                    {   // 连线时长 大于等于 5分钟 // 等待评价
                        $resData = [
                            'status' => 31,
                            'duration' => $rtcInfo['duration'],
                            'coin' => $rtcInfo['coin'],
                            'avatar' => $avatar,
                            'name' => $nickname,
                            'id' => $chatDetailId,
                        ];
                    } else
                    {
                        $resData = [
                            'status' => 32,
                            'duration' => $rtcInfo['duration'],
                            'coin' => $rtcInfo['coin'],
                            'id' => $chatDetailId,
                            'list' => [
                                [
                                    'title' => '本次消耗详情',
                                    'des' => '',
                                    'type' => 1,
                                    'target' => '',
                                    'link' => '',
                                ], [
                                    'title' => '服务编号',
                                    'des' => "$chatDetailId",
                                    'type' => 2,
                                    'target' => '',
                                    'link' => '',
                                ], [
                                    'title' => '咨询师单价',
                                    'des' => $rtcInfo['unit_price'] . '准币/分钟',
                                    'type' => 1,
                                    'target' => '',
                                    'link' => '',
                                ], [
                                    'title' => '连线时长',
                                    'des' => gmdate("H:i:s", $rtcInfo['duration']),
                                    'type' => 1,
                                    'target' => '',
                                    'link' => '',
                                ],
                            ],
                        ];

                        if (isset($rtcInfo['extend']['cid']))
                        {
                            $userCouponAttr = (new UserCoupon())->getInfoById($rtcInfo['extend']['cid']);
                            $desc = "免费{$userCouponAttr['discount']}分钟（优惠券）";
                            if ($userCouponAttr['condition'] > 0)
                            {
                                $desc = "满{$userCouponAttr['condition']}分钟，" . $desc;
                            }
                            $resData['list'][] = [
                                'title' => '优惠',
                                'des' => $desc,
                                'type' => 1,
                                'target' => '',
                                'link' => '',
                            ];
                        }

                        if (isset($rtcInfo['extend']['free_rtc_duration']) && $rtcInfo['extend']['free_rtc_duration'] > $rtcInfo['test_duration'])
                        {   // 连线福利
                            $resData['list'][] = [
                                'title' => '连线福利',
                                'des' => sprintf("免费%d分钟", (int)($rtcInfo['extend']['free_rtc_duration'] / 60)),
                                'type' => 1,
                                'target' => '',
                                'link' => '',
                            ];
                        }

                        if (isset($rtcInfo['extend']['gift_id'], $rtcInfo['extend']['gift_use_duration']) && $rtcInfo['extend']['gift_id'] && $rtcInfo['extend']['gift_use_duration'] > 0)
                        {   // 使用 礼包信息
                            $userGiftInfo = (new AppUserConsultGift())->getInfoById($rtcInfo['extend']['gift_id'], ['gift_id']);
                            $giftInfo = (new AppConsultGift())->getInfoById($userGiftInfo['gift_id'], ['name']);
                            $resData['list'][] = [
                                'title' => '礼包',
                                'des' => $giftInfo['name'],    // 礼包名称
                                'type' => 5,
                                'target' => 'local',
                                'link' => "consult_gift_pack_detail?id={$rtcInfo['extend']['gift_id']}&need_auth=2",
                            ];
                            $resData['list'][] = [
                                'title' => '礼包使用时长',
                                'des' => ceil($rtcInfo['extend']['gift_use_duration'] / 60) . '分钟',
                                'type' => 1,
                                'target' => '',
                                'link' => '',
                            ];
                        }

                        $resData['list'][] = [
                            'title' => '连线消耗',
                            'des' => $rtcInfo['coin'] . '准币',
                            'type' => 1,
                            'target' => '',
                            'link' => '',
                        ];
                        if (isset($rtcInfo['extend']['gift_id'], $rtcInfo['extend']['gift_use_duration']) && $rtcInfo['extend']['gift_id'])
                        {
                            $resData['gift_use_duration'] = $rtcInfo['extend']['gift_use_duration'];    // 礼包使用时长
                        } else
                        {
                            $resData['gift_use_duration'] = 0;
                        }
                    }
                    break;
                case 6:
                    $consultAccountInfo = (new Zlcr3ConsultAccount())->getInfoById($astrId);
                    $resData = [
                        'status' => 31,
                        'duration' => 0,
                        'coin' => 0,
                        'avatar' => $avatar,
                        'name' => $nickname,
                        'id' => $chatDetailId,
                        'level' => $consultAccountInfo['level'],
                    ];
                    break;
                default:
                    return show(config('code.error'), '服务异常，请稍后再试～');
            }
        } else
        {   // 当前 没有连麦
            $astrInfo = $zlcrAstrModel->getInfoById($astrId);
            $consultAccount = $zlcr3ConsultAccountModel->getInfoById($astrId);
            // 获取新人福利 免充值 发起连线
            $newUserWelfare = (new ZyhsConsultRtcUserBenefits())->getConsultRtcUserBenefits($this->appType2, $this->user['id']);
            $freeRtcDuration = $newUserWelfare ? (($newUserWelfare['min'] ?? 0) * 60) : 0;
            $appUserConsultGiftModel = new AppUserConsultGift();
            if (isset($userNowConsultRtcInfo) && $userNowConsultRtcInfo)
            {   // 当前正在连线
                if ((int)$userNowConsultRtcInfo['status'] === 1)
                {   //  1=>等待咨询师接受
                    $status = 12;
                    $btnText = '等待咨询师响应';
                } else
                {   //  2=>咨询师接受开始连麦中
                    $status = 13;
                    $btnText = '连接中';
                }
                if (isset($userNowConsultRtcInfo['gift_id']) && $userNowConsultRtcInfo['gift_id'])
                {
                    $userGiftInfo = $appUserConsultGiftModel->getInfoById($userNowConsultRtcInfo['gift_id']);
                    $consultGiftInfo = (new AppConsultGift())->getInfoById($userGiftInfo['gift_id'], ['name']);
                    $giftName = $consultGiftInfo['name'];
                } else
                {
                    $giftName = '';
                }
            } else
            {   // 当前没有连线信息
                if ($consultAccount['status'] === 1)
                {   // 在线
                    $status = 0;
                    if ($freeRtcDuration > 60)
                    {
                        $btnText = sprintf("免费咨询连线%d分钟", $newUserWelfare['min']);
                    } else
                    {
                        $btnText = '申请连线咨询（免费试麦60秒）';
                    }
                } else
                {   // 休息
                    $status = 1;
                    // $btnText = '咨询师休息中，请稍后再来';
                    $btnText = '休息中，请预沟通留言';
                }
                $GiftPageData = $appUserConsultGiftModel->getListPage($this->user['id'], 'id', [], 1);
                $hasGiftPack = $GiftPageData['data'] ? 1 : 0;
            }
            $tip = '';
            if ($consultAccount['service_duration'] > 0)
            {
                $t = ceil($consultAccount['service_duration'] / 60);
                $tip .= '服务' . ($t > 200 ? '200+' : $t) . '分钟';
            }
            if ($consultAccount['recommended_nums'] > 0)
            {
                if ($tip)
                {
                    $tip .= '·';
                }
                $tip .= "{$consultAccount['recommended_nums']}次被推荐";
            }

            $user_coin = $UserAccountModel->getUserCoinFromCache($this->user['id']);  // 账户信息
            $coinNum = (int)($user_coin['balance'] ?? 0);// 准币余额
            $coinPwdStatus = (isset($user_coin['password']) && $user_coin['password']) ? 1 : 0;  // 是否设置了支付密码

            $couponIds = (new UserCoupon())->getConsultRtcCids($this->user['id']);
            if (Util::ZyhsIsHuaweiOrHonorOrOppoHg(2))
            {
                $rtcConfig = (new ZyhsConfig())->configInfo('rtc');
                if (Util::IsHuaweiAndOppo())
                {
                    if (isset($rtcConfig['huawei_work_intro']) && $rtcConfig['huawei_work_intro'])
                    {
                        $workIntro = $rtcConfig['huawei_work_intro'];
                    }
                } else
                {
                    if (isset($rtcConfig['honor_work_intro']) && $rtcConfig['honor_work_intro'])
                    {
                        $workIntro = $rtcConfig['honor_work_intro'];
                    }
                }
            }
            $tags = $zlcr3ConsultAccountModel->getTags($consultAccount['tags']);
            if (Util::ZyhsIsHuaweiOrHonorOrOppoHg(2))
            {
                $tags = array_values(array_diff($tags, ['星盘']));
            }
            $resData = [
                'status' => $status,
                'avatar' => $astrInfo['avatar'],
                'name' => $astrInfo['nickname'],
                'tags_icon' => $zlcr3ConsultAccountModel->getTagsIcon($consultAccount['tids']),
                'tags' => $tags,
                'user_coin' => $coinNum,
                'is_set_password' => $coinPwdStatus,
                'price_coin' => $consultAccount['price'],
                'tip' => $tip,
                'notice' => "你已确认自己为成年人，无精神疾病和自我伤害等倾向，并承诺在咨询过程中理性看待老师给出的建议，在咨询以外的一切其它行为与平台无关。\r\n连线需保证您的准币余额大于五分钟连麦消耗。\r\n请提前添加咨询所需的档案信息，以免浪费咨询时间。\r\n连麦开始1分钟内是免费试麦准备时间，如果出现声音问题请及时申请故障挂断，调试后重试。\r\n请保证网络正常，以免因网络问题影响你的咨询体验\r\n连麦咨询不支持退款，请谨慎购买，理性消费:为保证咨询服务的完整性，请尽量保证准币余额充足。",
                'des' => $workIntro ?? ($astrInfo['extend']['work_intro'] ?? ''),    // 营业介绍
                'btn_text' => $btnText,
                'id' => $chatDetailId ?? 0,
                'has_coupon' => $couponIds ? 1 : 0,
                'level' => $consultAccount['level'],
                'has_astro_skill' => in_array(116, $consultAccount['tags'], false) ? 1 : 0, // 是否具备 星盘 资格 1=>具备 0=>不具备
                'free_rtc_duration' => $freeRtcDuration,
            ];
            if (isset($hasGiftPack))
            {
                $resData['has_gift_pack'] = $hasGiftPack;
            }
            if (isset($giftName))
            {
                $resData['gift_name'] = $giftName;
            }
        }

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 咨询详情
     * @return Json
     * @throws \Exception
     */
    public function rtcChatDetail()
    {
        $chatDetailId = input('id/d', 0);
        $type = input('type/d', 0);//1=>完整 2=>精简【弹窗评价页面进入】
        $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();

        $chatDetail = $consultChatRecordDetailsModel->getInfoById($chatDetailId);
        $rtcId = $chatDetail['extend']['rtc_data']['rtc_id'] ?? 0;
        if (!$chatDetail || $chatDetail['uid'] !== $this->user['id'] || !$rtcId)
        {
            return show(config('code.error'), '服务异常，咨询信息不存在～');
        }
        $data = (new ZyhsConsultChatRtcRecord())->getInfoById($rtcId);

        $astrInfos = (new ZlcrAstr())->getInfoById($data['astr_id'], ['nickname', 'avatar']);

        $couponInfoData = null;
        if (isset($data['extend']['cid']))
        {
            $userCouponAttr = (new UserCoupon())->getInfoById($data['extend']['cid']);
            $desc = "免费{$userCouponAttr['discount']}分钟（优惠券）";
            if ($userCouponAttr['condition'] > 0)
            {
                $desc = "满{$userCouponAttr['condition']}分钟，" . $desc;
            }
            $couponInfoData = [
                'title' => '优惠',
                'des' => $desc,
                'type' => 1,
                'target' => '',
                'link' => '',
            ];
        }
        // 连线福利【新人福利】
        $rtcBenefits = null;
        if (isset($data['extend']['free_rtc_duration']) && $data['extend']['free_rtc_duration'] > $data['test_duration'])
        {   // 连线福利
            $rtcBenefits = [
                'title' => '连线福利',
                'des' => sprintf("免费%d分钟", (int)($data['extend']['free_rtc_duration'] / 60)),
                'type' => 1,
                'target' => '',
                'link' => '',
            ];
        }

        if ($type === 1)
        {
            $comment = $data['is_comment'] ? (new ZyhsConsultChatRtcRecordComment())->getInfoById($data['id'], ['level']) : null;

            $res = [
                'title' => '本次消耗详情',
                'list' => [
                    [
                        'title' => '服务编号',
                        'des' => $data['chat_detail_id'] ?? 0,
                        'type' => 2,
                        'target' => '',
                        'link' => '',
                    ],
                    [
                        'title' => '申请时间',
                        'des' => date('Y-m-d H:i:s', $data['create_time']),
                        'type' => 1,
                        'target' => '',
                        'link' => '',
                    ],
                    [
                        'title' => '咨询师',
                        'des' => $astrInfos['nickname'],
                        'type' => 1,
                        'target' => '',
                        'link' => '',
                    ],
                    [
                        'title' => '咨询师单价',
                        'des' => $data['unit_price'] . '准币/分钟',
                        'type' => 1,
                        'target' => '',
                        'link' => '',
                    ],
                    [
                        'title' => '连线时长',
                        'des' => gmdate("H:i:s", $data['duration']),
                        'type' => 1,
                        'target' => '',
                        'link' => '',
                    ],
                ],
            ];
            if ($couponInfoData)
            {   // 优惠券信息
                $res['list'][] = $couponInfoData;
            }
            if ($rtcBenefits)
            {   // 连线福利
                $res['list'][] = $rtcBenefits;
            }
            if (isset($data['extend']['gift_id'], $data['extend']['gift_use_duration']) && $data['extend']['gift_id'] && $data['extend']['gift_use_duration'] > 0)
            {   // 使用 礼包信息
                $userGiftInfo = (new AppUserConsultGift())->getInfoById($data['extend']['gift_id'], ['gift_id']);
                $giftInfo = (new AppConsultGift())->getInfoById($userGiftInfo['gift_id'], ['name']);
                $res['list'][] = [
                    'title' => '礼包',
                    'des' => $giftInfo['name'],    // 礼包名称
                    'type' => 5,
                    'target' => 'local',
                    'link' => "consult_gift_pack_detail?id={$data['extend']['gift_id']}&need_auth=2",
                ];
                $res['list'][] = [
                    'title' => '礼包使用时长',
                    'des' => ceil($data['extend']['gift_use_duration'] / 60) . '分钟',
                    'type' => 1,
                    'target' => '',
                    'link' => '',
                ];
            }
            $res['list'][] = [
                'title' => '计费时长',
                'des' => ceil($data['eff_duration'] / 60) . '分钟',
                'type' => 1,
                'target' => '',
                'link' => '',
            ];
            $res['list'][] = [
                'title' => '连线消耗',
                'des' => $data['coin'] . '准币',
                'type' => 1,
                'target' => '',
                'link' => '',
            ];
            if ($data['status'] === 4)
            {   // 正常挂断
                $res['list'][] = [
                    'title' => '评价',
                    'des' => $data['is_comment'] ? $comment['level'] : '前往评价',
                    'type' => $data['is_comment'] ? 3 : 4,
                    'target' => '',
                    'link' => '',
                ];
                if ($data['is_recommend'] === 1)
                {
                    $res['list'][] = [
                        'title' => '推荐',
                        'des' => '已推荐',
                        'type' => 1,
                        'target' => '',
                        'link' => '',
                    ];
                }
            }
        } else
        {
            $res = [
                'title' => '本次消耗详情',
                'list' => [
                    [
                        'title' => '服务编号',
                        'des' => $data['chat_detail_id'] ?? 0,
                        'type' => 2,
                        'target' => '',
                        'link' => '',
                    ],
                    [
                        'title' => '咨询师单价',
                        'des' => $data['unit_price'] . '准币/分钟',
                        'type' => 1,
                        'target' => '',
                        'link' => '',
                    ],
                    [
                        'title' => '连线时长',
                        'des' => gmdate("H:i:s", $data['duration']),
                        'type' => 1,
                        'target' => '',
                        'link' => '',
                    ],
                ],
            ];
            if ($couponInfoData)
            {
                $res['list'][] = $couponInfoData;
            }
            if ($rtcBenefits)
            {   // 连线福利
                $res['list'][] = $rtcBenefits;
            }
            $res['list'][] = [
                'title' => '计费时长',
                'des' => ceil($data['eff_duration'] / 60) . '分钟',
                'type' => 1,
                'target' => '',
                'link' => '',
            ];
            $res['list'][] = [
                'title' => '连线消耗',
                'des' => $data['coin'] . '准币',
                'type' => 1,
                'target' => '',
                'link' => '',
            ];
        }
        $res['nickname'] = $astrInfos['nickname'];
        $res['avatar'] = $astrInfos['avatar'];

        return show(config('code.success'), '请求成功', $res);
    }
}
